package org.apache.lucene;

//import javax.swing.JApplet;
import java.io.IOException;

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.misc.HighFreqTerms;
//import org.apache.lucene.misc.TermStats;
//import org.apache.lucene.misc.TermStats;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.NoLockFactory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

//import wviz.Constants;
//import wviz.gui.TermStats;

////import prefuse.util.ui.JPrefuseApplet;

//import wviz.gui.LeonardoMainFrame;
//import wviz.gui.T2vPanel;

public class LuceneAppletTest extends JApplet {
	// Called when this applet is loaded into the browser.
	public void init() {
		// Execute a job on the event-dispatching thread; creating this applet's
		// GUI.
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					try {
						createPanel();
						//createIndex();
					} catch (CorruptIndexException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.err.println("createGUI didn't complete successfully " + e);
					} catch (LockObtainFailedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.err.println("createGUI didn't complete successfully " + e);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.err.println("createGUI didn't complete successfully " + e);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.err.println("createGUI didn't complete successfully " + e);
					}
				}
			});
		} catch (Exception e) {
			System.err.println("createGUI didn't complete successfully " + e);
		}
	}
	
	private void createPanel() throws CorruptIndexException, LockObtainFailedException, IOException, ParseException{
	  JPanel newContentPane = new JPanel();
	  newContentPane.add(new JTextArea("tt"));
        //newContentPane.setOpaque(true); 
        setContentPane(newContentPane);  
	//}
        newContentPane.add(new JTextArea("before analyz"));

	//private void createIndex() throws CorruptIndexException, LockObtainFailedException, IOException, ParseException {
		
				//new StandardAnalyzer(Version.LUCENE_35);
		 newContentPane.add(new JTextArea("after analy"));
	    // 1. create the index
	    Directory index = new RAMDirectory();
	    index.createOutput("zoh");
		index.setLockFactory(NoLockFactory.getNoLockFactory()  );
		 newContentPane.add(new JTextArea("after index"));
		 
		 SimpleAnalyzer analyzer = new SimpleAnalyzer(Version.LUCENE_35);
		 newContentPane.add(new JTextArea("after analyz"));
	    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
	    newContentPane.add(new JTextArea("after index config"));
	    IndexWriter w = new IndexWriter(index, config);
	    newContentPane.add(new JTextArea("after writer"));
	    addDoc(w, "Lucene in Action");
	    addDoc(w, "Lucene for Dummies");
	    addDoc(w, "Managing Gigabytes");
	    addDoc(w, "The Art of Computer Science");	    
	    w.close();
	   
	 //   TermStats[] ts =HighFreqTerms.getHighFreqTerms(IndexReader.open(index), 5, "contents");

	    // 2. query
	    //String querystr = args.length > 0 ? args[0] : "lucene";
	    String querystr = "lucene";

	    // the "title" arg specifies the default field to use
	    // when no field is explicitly specified in the query.
	    Query q = new QueryParser(Version.LUCENE_35, "title", analyzer).parse(querystr);

	    // 3. search
	    int hitsPerPage = 10;
	    IndexSearcher searcher = new IndexSearcher(index, true);
	    TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
	    searcher.search(q, collector);
	    ScoreDoc[] hits = collector.topDocs().scoreDocs;
	    
	    // 4. display results
	    System.out.println("Found " + hits.length + " hits.");
	    for(int i=0;i<hits.length;++i) {
	      int docId = hits[i].doc;
	      Document d = searcher.doc(docId);
	      System.out.println((i + 1) + ". " + d.get("title"));
	      newContentPane.add(new JTextArea(d.get("title")));
	    }

	    // searcher can only be closed when there
	    // is no need to access the documents any more. 
	    searcher.close();
	  }

	  private static void addDoc(IndexWriter w, String value) throws IOException {
	    Document doc = new Document();
	    doc.add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED));
	    w.addDocument(doc);
	  }
	}
